﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Xml.Linq;

namespace Lastfm7.ViewModels
{
    public class ProfileViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<MenuItemViewModel> MenuItems { get; private set; }

        public ArtistListViewModel TopArtistsModel { get; private set; }
        public ArtistListViewModel RecommendedArtistsModel { get; private set; }

        private string username = string.Empty;
        private string country = string.Empty;
        private string gender = string.Empty;
        private Uri avatarUrl;
        private int playCount;
        private int age;

        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                PropertyChanged.Raise(this, "Username");
            }
        }

        public string Country
        {
            get { return country; }
            set
            {
                country = value;
                PropertyChanged.Raise(this, "Country");
            }
        }

        public string Gender
        {
            get { return gender; }
            set
            {
                gender = value;
                PropertyChanged.Raise(this, "Gender");
            }
        }

        public Uri AvatarUrl
        {
            get { return avatarUrl; }
            set
            {
                avatarUrl = value;
                PropertyChanged.Raise(this, "AvatarUrl");
            }
        }

        public int PlayCount
        {
            get { return playCount; }
            set
            {
                playCount = value;
                PropertyChanged.Raise(this, "PlayCount");
            }
        }

        public int Age
        {
            get { return age; }
            set
            {
                age = value;
                PropertyChanged.Raise(this, "Age");
            }
        }


        public ProfileViewModel()
        {
            CreateItems();
        }

        public ProfileViewModel(XElement userInfo)
        {
            var largeAvatar = (from img in userInfo.Elements("image")
                               where img.Attribute("size").Value == "extralarge"
                               select img.Value).SingleOrDefault();
             
            Username = userInfo.Element("name").Value.ToLower();
            Country = userInfo.Element("country").Value.ToLower();
            Gender = userInfo.Element("gender").Value.ToLower();
            PlayCount = int.Parse(userInfo.Element("playcount").Value);
            Age = int.Parse(userInfo.Element("age").Value);
            AvatarUrl = new Uri(largeAvatar);

            CreateItems();
        }

        void CreateItems()
        {
            TopArtistsModel = new TopArtistsViewModel();
            RecommendedArtistsModel = new RecommendedArtistsViewModel();

            MenuItems = new ObservableCollection<MenuItemViewModel> {
                TopArtistsModel,
                RecommendedArtistsModel,
                new MenuItemViewModel("events", "Find out about the music life in your city.")
              };
        }
    }
}
